const { app, BrowserWindow } = require("electron");
const path = require("path");

function createWindow() {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, "preload.js"),
      nodeIntegration: true,
      contextIsolation: false,
    },
  });
  // 直接加载 Vite 地址
  win.loadURL("http://localhost:3000");

  win.webContents.setWindowOpenHandler(({ url, frameName }) => {
    console.log(url, frameName)
    return { action: 'allow' };
    // if (url.startsWith('https://your-domain.com')) {
    //   // 在当前窗口打开
    //   return { action: 'allow' };
    // } else if (url.startsWith('https://external.com')) {
    //   // 阻止打开
    //   return { action: 'deny' };
    // } else {
    //   // 创建自定义新窗口
    //   return {
    //     action: 'new-window',
    //     overrideBrowserWindowOptions: {
    //       width: 1200,
    //       height: 800,
    //       title: 'External Link',
    //       webPreferences: { contextIsolation: true }
    //     }
    //   };
    // }
  });
}

app.whenReady().then(() => {
  createWindow();
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});
